from selenium import webdriver
from tools.getlog import Logger
class BasePage:
    def __init__(self,driver):
        self.driver=driver
        self.logger=Logger()
    def get_element(self,locator,name):
        try:
            ele=self.driver.find_element(*locator)
            # print("获取元素%s成功"%str(locator))
            self.logger.info("获取元素%s成功"%str(locator))
        except Exception as e:
            # print("获取元素%s失败"%str(locator))
            self.logger.error("获取元素%s失败"%str(locator))
            self.get_screen(name)
        return ele
    def input(self,locator,text,name):
        ele=self.get_element(locator,name)
        if ele != None:
            try:
                ele.send_keys(text)
                # print("文本输入成功")
                self.logger.info("文本输入成功")
            except Exception as e:
                print("文本输入失败")
                self.get_screen(name)
        else:
            print('元素获取为空')
    def click(self,locator,name):
        ele=self.get_element(locator,name)
        if ele != None:
            try:
                ele.click()
                # print("文本输入成功")
                self.logger.info("文本输入成功")
            except Exception as e:
                print("文本输入失败")
                self.get_screen(name)
        else:
            print('元素获取为空')

    def get_screen(self,name):
        self.driver.get_screenshot_as_file('../test_report/pic/'+name+'.png')